package com.bupt.smart.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.bupt.smart.model.User;
import com.google.gson.Gson;

/**
 * Created by ght on 2016/6/21.
 */
public class SaveSharedPreference {
    private static final String PREF_USER_INFO= "user_info";
    private static final String PREF_USER_LOGIN_STATUS= "user_login_status";

    static SharedPreferences getSharedPreferences(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    public static void setUserInfo(Context context, User userInfo) {
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        Gson gson = new Gson();
        editor.putString(PREF_USER_INFO, gson.toJson(userInfo));
        editor.commit();
    }

    public static User getUserInfo(Context context) {
        String userInfo = getSharedPreferences(context).getString(PREF_USER_INFO, "");
        Gson gson = new Gson();
        return gson.fromJson(userInfo, User.class);
    }

    public static void setUserLoginStatus(Context context, Boolean status) {
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putBoolean(PREF_USER_LOGIN_STATUS, status);
        editor.commit();
    }

    public static Boolean getUserLoginStatus(Context context) {
        return getSharedPreferences(context).getBoolean(PREF_USER_LOGIN_STATUS, false);
    }

    public static void clear(Context context)
    {
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.clear(); //clear all stored data
        editor.commit();
    }
}
